//package com.hydra.auth.config;
//
//import com.hydra.auth.common.UserRoleEnum;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.security.access.AccessDecisionManager;
//import org.springframework.security.access.AccessDeniedException;
//import org.springframework.security.access.ConfigAttribute;
//import org.springframework.security.authentication.InsufficientAuthenticationException;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.web.FilterInvocation;
//
//import java.util.Collection;
//
///**
// * 自定义权限匹配规则
// * @Author 陈煜敏
// * @Date 2022/9/2 15:50
// */
//@Slf4j
//public class SecurityUrlDecisionManager implements AccessDecisionManager {
//
//    @Override
//    public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes) throws AccessDeniedException, InsufficientAuthenticationException {
//        //获取请求的url
//        String requestUrl = ((FilterInvocation) object).getRequestUrl();
//
//        for(ConfigAttribute configAttribute: configAttributes){
//            //需要的权限
//            String needRole = configAttribute.getAttribute();
//            //需要的权限
//            UserRoleEnum role = UserRoleEnum.find(needRole);
//            boolean result = false;
//            //当前具备的用户权限
//            Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
//            for (GrantedAuthority authority : authorities) {
//                UserRoleEnum curRole = UserRoleEnum.find(authority.getAuthority());
//                if(curRole.getValue() >= role.getValue()){
//                    result = true;
//                }
//            }
//            if(!result){
//                log.error("权限不足：请求路径：{}, 当前权限：{}, 需求权限：{}", requestUrl, authorities, needRole);
//                throw new AccessDeniedException("权限不足！您当前暂无权限访问该模块");
//            }
//        }
//    }
//
//    @Override
//    public boolean supports(ConfigAttribute attribute) {
//        return false;
//    }
//
//    @Override
//    public boolean supports(Class<?> clazz) {
//        return false;
//    }
//}
